package com.xuecheng.auth.domain.strategy;

import com.xuecheng.auth.domain.model.dto.XcUserDto;
import com.xuecheng.auth.domain.model.vo.req.AuthParamsQuery;
import com.xuecheng.auth.domain.service.XcUserService;
import com.xuecheng.base.exception.XueChengPlusException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-09-07  15:20
 */
@Slf4j
public class AuthContext {

    private AuthStrategy authStrategy;
    private AuthParamsQuery authParamsQuery;

    public AuthContext(AuthParamsQuery authParamsQuery) {
        AuthTypeEnum authTypeEnum = AuthTypeEnum.of(authParamsQuery.getAuthType());
        if (Objects.isNull(authTypeEnum)){
            throw new XueChengPlusException("认证策略不存在");
        }
        try {
            this.authStrategy = authTypeEnum.getAuthStrategy().newInstance();
            this.authParamsQuery = authParamsQuery;
        } catch (Exception e){
            log.error("未知错误, {}", e.getMessage());
            throw new XueChengPlusException("未知错误");
        }
    }

    public XcUserDto handleAuth(){
        return authStrategy.execute(authParamsQuery);
    }
}
